﻿namespace BEGDC.CC.Web.UserControls.OCV
{
	using System;
	using System.Data;
	using System.Collections;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Text;

	using BEGDC.CC.BL.CCFlow.OCV;
	using BEGDC.CC.BL.CCFlow.CR;
	using BEGDC.CC.Entity;
	using BEGDC.CC.BL.Workflow;
	using BEGDC.CC.BL.Common;
	using BEGDC.CC.BL.Common.Query;
	using BEGDC.CC.BL.UserManagement;
	using BEGDC.Utility.Castle;
	using BEGDC.Utility.ExceptionManagement;

	/// <summary>
	///	Summary description for Apply.
	/// </summary>
	public class Apply : BaseOCV
	{
		#region ui control definition
        protected System.Web.UI.WebControls.TextBox tbLvApprover;
		protected BEGDC.CC.Web.UserControls.Export ctrlExport;
		protected System.Web.UI.WebControls.Label Lb_CISWarning;
		protected System.Web.UI.WebControls.Label lbTitle;
		protected System.Web.UI.WebControls.Button btnSave;
		protected System.Web.UI.WebControls.Button btnSubmit;
		protected System.Web.UI.WebControls.Button btnReply;
		protected System.Web.UI.WebControls.Button btnCopy;
        protected System.Web.UI.WebControls.Button btnClose;
		protected System.Web.UI.WebControls.TextBox tbComment;
        //protected BEGDC.CC.Web.UserControls.CV.CVPrintCtrl cvPrintCtrl;

		protected BEGDC.CC.Web.UserControls.OCV.OCVCtrl uclOCV;
		private bool _isReadOnly = false;
		public bool isReadOnly
		{
			set
			{
				_isReadOnly = value;
			}
			get
			{
				return _isReadOnly;
			}
		}
		#endregion

		#region variable define
		private bool IsQuery
		{
			get
			{
				string state = this.Request.QueryString["State"];
				if(state == null) return false;
				return state == SysDicItem.ProjectStatusFlag.QUERY;
			}
		}
		private bool IsView
		{
			get
			{
				string state = this.Request.QueryString["State"];
				if(state == null) return false;
				if(state == SysDicItem.ProjectStatusFlag.View || state == SysDicItem.ProjectStatusFlag.CANCEL
					|| state == SysDicItem.ProjectStatusFlag.Pending || state == SysDicItem.ProjectStatusFlag.End
					|| state == SysDicItem.ProjectStatusFlag.Participation)
					return true;
				else
					return false;
			}
		}
		protected string Step
		{
			get
			{
				IQuery queryService = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.QueryService] as IQuery;
				FlowInstance flowInstance = queryService.QueryFlowInstance( this.AppID );
				if( flowInstance == null )
					return WorkTransition.Draft;
				else
					return flowInstance.CurrentActiveStep.StepCode;
			}
		}
		protected string ApplicationNo
		{
			get
			{ 
				if( this.ViewState["ApplicationNo"] == null )
					return string.Empty;
				return this.ViewState["ApplicationNo"].ToString(); 
			}
			set{ this.ViewState["ApplicationNo"] = value; }
		}
		
		protected System.Web.UI.WebControls.TextBox Textbox1;
		protected System.Web.UI.WebControls.Label lbIsSensitiveIssue;
		protected System.Web.UI.HtmlControls.HtmlInputHidden hiddenScrollHight;
		protected System.Web.UI.WebControls.Label lblLegalEntity;
		protected System.Web.UI.WebControls.Label lbDepartmentReadOnly;
		protected System.Web.UI.WebControls.Label lbIsSSMEReadOnly;
		protected System.Web.UI.WebControls.DropDownList ddlCostCenter;
		protected System.Web.UI.WebControls.Label lbCostCenterReadOnly;
		protected System.Web.UI.WebControls.DropDownList ddlDepartment;
		protected System.Web.UI.HtmlControls.HtmlTableRow TR_CounselApprovalTitle;
		protected System.Web.UI.HtmlControls.HtmlTableRow TR_CounselApproval;
		protected System.Web.UI.WebControls.RadioButtonList rbIsSSME;
		#endregion

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if(!Page.IsPostBack)
			{
				this.btnReply.Attributes.Add("onclick", "javascript:OpenApplyReplyPane("+Request.QueryString["FlowID"]+");");
				this.btnSubmit.Attributes.Add("onclick", "javascript:OpenAssignmentPane();");
                this.btnClose.Attributes.Add("onclick", "javascript:SimpleCloseWindow();");
			}
			ButtonStatusSetting();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnReply.Click += new System.EventHandler(this.btnReply_Click);
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
			this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		
		private void btnSave_Click(object sender, EventArgs e)
		{
			this.uclOCV.Save();
			this.uclOCV.AfterSave();
		}
		
		private void btnSubmit_Click(object sender, EventArgs e)
		{
			try
			{
				string msg;
				this.uclOCV.Save();
				if(this.AppID == 0)
					this.AppID = this.uclOCV.GetApplication().Id;
				msg = BEGDC.CC.BL.CCFlow.AttachmentService.GetMissedAttachmentMessage( this.AppID );
				if( msg!="" && msg!=string.Empty)
				{
					Page.RegisterClientScriptBlock("","<script>alert('" + msg + "');</script>");
					return;
				}
				IOCV ocvService = AppContainer.Instance[ServiceConst.OCVService] as IOCV;

				if(this.uclOCV.DatasValidation())
				{
					this.uclOCV.Save();
					Application app = this.uclOCV.GetApplication();
					if(app.AppNo.IndexOf("-D") >= 0)
						app.AppNo = string.Empty;

                    //Search Lv4Approver
                    BEGDC.CC.BL.UserManagement.IUser userService = AppContainer.Instance[ServiceConst.UserService] as BEGDC.CC.BL.UserManagement.IUser;
                    Employee lv4Approver = userService.GetUserByID(this.tbLvApprover.Text);

					ocvService.Request(app ,lv4Approver);
					Page.RegisterClientScriptBlock("","<script>VbAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');</script><script>window.close();</script>");
				}
			}
			catch(Exception exp)
			{
				if (exp.InnerException !=null)
					Page.RegisterClientScriptBlock("","<script>alert('" + exp.InnerException.Message + "');</script>");
				else
					Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
			}
		}

		private void btnCopy_Click(object sender, EventArgs e)
		{
			try
			{
				IOCV ocvService = AppContainer.Instance[ServiceConst.OCVService] as IOCV;
				ocvService.CopyOCV(this.AppID);
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
			}

			Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM004 + "');</script>");
		}

		protected override void ButtonStatusSetting()
		{			
			if( this.Access == null )
				return;

			this.btnSubmit.Visible = this.Access.IsButtonVisible(OCVTransitionList.Submit);
			this.btnSave.Visible = this.Access.IsButtonVisible(OCVTransitionList.Save);
			this.btnReply.Visible = this.Access.IsButtonVisible(OCVTransitionList.Reply);
			this.btnCopy.Visible = this.Access.IsButtonVisible(OCVTransitionList.Copy);
            this.uclOCV.cvPrintCtrl.ProcessType = WorkflowTypes.OCV;
            this.uclOCV.cvPrintCtrl.Visible = this.Access.IsButtonVisible(OCVTransitionList.Copy);

//			if( (this.Access.IsHideAllButton) || (this.Access.IsReadOnly && !this.IsQuery) || (this.Step != WorkTransition.Draft
//				&& this.Step != WorkTransition.CV_Apply && this.Step != WorkTransition.CV_ReApply))
//			{
//				tr_Title.Visible = false;
//			}
		
		}

		private void btnReply_Click(object sender, EventArgs e)
		{
			try
			{
				this.uclOCV.Save();
				IOCV ocvService = AppContainer.Instance[ServiceConst.OCVService] as IOCV;
				ocvService.ReplyQuery(Convert.ToInt64(Request.QueryString["FlowID"]), WebUtility.DeCodeString(Server, tbComment.Text));
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');window.close();</script>");
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
			}
		}

	}
}
